<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 工作排班
                </el-breadcrumb-item>
                <el-button style="margin-left: 90%"
                           type="text"
                           icon="el-icon-refresh"
                           @click="getData"
                >刷新
                </el-button>
            </el-breadcrumb>

        </div>
        <div class="container">
            <div class="handle-box">
                <el-button
                    type="primary"
                    icon="el-icon-delete"
                    class="handle-del mr10"
                    @click="delAllSelection"
                >批量删除
                </el-button>
                <el-date-picker
                    @change="changeDate()"
                    v-model="query.date"
                    type="month"
                    placeholder="排班月份" style="margin-right: 5px;width: 16%">
                </el-date-picker>
                <el-input size="small" style="margin-right: 5px;width: 16%" prefix-icon="el-icon-search"
                          v-model="query.name" placeholder="请输入姓名" class="handle-input mr10"></el-input>

                <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
                <el-button type="primary" icon="el-icon-circle-plus" @click="handleAdd">添加排班</el-button>
                <el-button type="primary" @click="HandlerDialogArrange()">导出排班<i
                    class="el-icon-download el-icon--right"></i></el-button>
            </div>
            <el-table
                :data="officeTable"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
                @selection-change="handleSelectionChange">
                <el-table-column type="selection" width="55" align="center"></el-table-column>
                <el-table-column prop="id" label="ID" width="55" align="center">
                    <template slot-scope="scope">
                        {{ scope.$index + 1 }}
                    </template>
                </el-table-column>
                <el-table-column prop="name" label="姓名" align="center"></el-table-column>
                <el-table-column prop="date" label="排班月份" align="center">
                    <template slot-scope="scope">{{ scope.row.date| dateMonthFormat }}</template>
                </el-table-column>
                <el-table-column prop="addTime" label="添加时间" align="center"></el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.row.id,scope.row.date)"
                        >编辑
                        </el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.row.id)"
                        >删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageIndex"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>
        <!--         添加弹窗 -->
        <el-dialog title="添加排班" :visible.sync="addVisible" width="30%">
            <el-form ref="job" :model="job" label-width="70px">
                <el-form-item label="姓名">
                    <el-select v-model="job.userId" placeholder="请选择">
                        <el-option v-for="val in selectName" :key="val.userId" :value="val.id" :label="val.name" />
                    </el-select>
                </el-form-item>
                <el-form-item label="排班周期" style="width: 284px">
                    <el-input type='number' maxLength='2' v-model="job.period"></el-input>
                </el-form-item>
                <el-form-item label="休息时间">
                    <el-date-picker
                        type="date"
                        placeholder="选择日期"
                        v-model="job.restDate"
                        value-format="yyyy-MM-dd"
                        style="width: 100%;">
                    </el-date-picker>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="cancel()">取 消</el-button>
                <el-button type="primary" @click="saveJobTable">确 定</el-button>
            </span>
        </el-dialog>
        <!--        编辑弹窗-->
        <el-dialog title="编辑排班" :visible.sync="editVisible" width="60%">
            <el-calendar id="calendar" v-model="value">
                <template
                    slot="dateCell"
                    slot-scope="{date, data}"
                >
                    <div @click="handleAddClick(data.day)">

                        <div class="data.isSelected ? 'is-selected' : ''">
                            {{ data.day.split('-').slice(2).join('-') }}
                        </div>
                        <div v-for="item in attendanceDetailsData">
                            <div v-if="item.time===data.day+''" :style="item.style+vv"
                                 style="font-size: 16px;font-weight: inherit">
                                {{ item.data }}
                            </div>
                        </div>
                    </div>
                </template>
            </el-calendar>

        </el-dialog>
        <!--        修改工作状态弹窗-->
        <el-dialog title="修改工作状态" :visible.sync="dialogFormVisible" width="20%">
            <el-form>
                <el-form-item label="工作状态">
                    <el-select v-model="jobV.state" placeholder="请选择">
                        <el-option key="1" label="上班" value="1"></el-option>
                        <el-option key="2" label="休息" value="2"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="saveJobState">确 定</el-button>
                </span>
        </el-dialog>

        <el-dialog
            title="导出条件"
            :visible.sync="dialogArrange"
            width="30%">
            <div class="block" style="margin-left: 5%">
                <span class="demonstration">选择导出时间：</span>
                <el-date-picker
                    v-model="exportTime"
                    type="month"
                    placeholder="输入排班月份">
                </el-date-picker>
            </div>
            <span slot="footer" class="dialog-footer">
    <el-button @click="dialogArrange = false">取 消</el-button>
    <el-button type="primary" @click="downloadFile()">确 定</el-button>
  </span>
        </el-dialog>


    </div>
</template>

<script>
import {
    getListOfficeTable,
    downloadOfficeTable,
    updateOfficeTable,
    getJobState,
    getOfficeTableInfo,
    delOfficeTable,
    addPersonalOfficeTable
} from '@/api/job';
import { getAllUser } from '@/api/staff';

export default {
    name: 'basetable',
    data() {
        return {
            value: 'Sat Oct 17 2020 09:16:36 GMT+0800',
            query: {
                date: new Date(),
                pageIndex: 1,
                pageSize: 10
            },
            officeTable: [],
            multipleSelection: [],
            addVisible: false,
            editVisible: false,
            pageTotal: 0,
            selectName: [],
            job: {
                birthday: new Date(),
                sex: 0,
                money: 0
            },
            ids: {
                ids: []
            },
            vv: ';text-align: right;margin-top: 32px',
            //text-align: right;margin-top: 32px;
            attendanceDetailsData: [],
            dialogFormVisible: false,
            dialogArrange: false,
            jobId: '',
            jobSate: {},
            jobV: {
                state: ''
            },
            updateJobState: {}, //修改工作状态
            exportInfo: {},
            exportTime: ''//导出时间
        };
    },
    created() {
        this.getData();
    },
    methods: {
        HandlerDialogArrange() {
            this.exportTime = '';
            this.dialogArrange = true;
        },
        downloadFile() {
            this.exportInfo.exportTime = this.exportTime;
            downloadOfficeTable(this.exportInfo).then(res => {
                if (res.success) {
                    // 创建a标签
                    const link = document.createElement('a');
                    link.setAttribute('href', res.data);
                    link.click();
                    this.dialogArrange = false;
                    this.$message.success('已经为您导出');
                } else {
                    this.$message.warning(res.message);
                }


            });

        },


        changeDate() {
            this.getData();

        },

        //保存工作状态信息
        saveJobState() {
            this.updateJobState.state = this.jobV.state;
            this.updateJobState.id = this.jobSate.id;
            this.updateJobState.date = this.jobSate.date;
            updateOfficeTable(this.updateJobState).then(res => {
                if (res.success) {
                    this.dialogFormVisible = false;
                    this.$message.success('修改成功');
                    this.handleEdit(this.updateJobState.id);
                } else {
                    this.$message.warning(res.message);
                }
            });
        },
        //打开修改工作状态弹窗
        handleAddClick(date) {
            this.dialogFormVisible = true;
            this.jobSate.date = date;
            getJobState(this.jobSate).then(res => {
                this.jobV.state = res.data;

            });


        },
        //编辑弹窗
        handleEdit(id, date) {
            this.value = date;
            getOfficeTableInfo(id).then(res => {
                if (res.success) {
                    this.jobSate.id = id;
                    this.editVisible = true;
                    this.attendanceDetailsData = res.data;
                } else {
                    this.$message.error(res.message);

                }
            });

        },

        //保存排班表
        saveJobTable() {

            addPersonalOfficeTable(this.job).then(res => {
                if (res.success) {
                    this.$message.success('添加成功');
                    this.addVisible = false;
                    this.getData();
                } else {
                    this.$message.warning(res.message);
                }
            });


        },

        //打开添加弹窗
        handleAdd() {
            this.job = {};
            this.addVisible = true;
            //获取用户姓名和id
            getAllUser().then(res => {
                this.selectName = res.data;
            });


        },
        // 获取列表
        getData() {
            getListOfficeTable(this.query).then(res => {
                if (res.success) {
                    this.officeTable = res.data.records;
                    this.pageTotal = res.data.total || 0;
                } else {
                    this.$message.warning(res.message);
                    if (res.code === 10005) {
                        this.$router.push({ path: '/login' });
                    }
                }

            });
        },
        //取消按钮
        cancel() {
            this.addVisible = false;
            //清空对象
            this.tableData = {};
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(userId) {
            if (userId !== undefined) {
                this.ids.ids = [];
            }
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            }).then(() => {
                this.ids.ids.push(userId);
                delOfficeTable(this.ids).then(res => {
                    if (res.success) {
                        this.$message.success('删除成功');
                        this.getData();
                    } else {
                        this.$message.error(res.message);
                    }

                });
            }).catch(() => {
            });
        },
        // 多选操作
        handleSelectionChange(val) {
            this.multipleSelection = val;
        },
        delAllSelection() {
            for (let i = 0; i < this.multipleSelection.length; i++) {
                this.ids.ids.push(this.multipleSelection[i].id);

            }
            this.handleDelete();


        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageIndex', val);
            this.getData();
        }
    }
};
</script>

<style scoped>

.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 200px;
    display: inline-block;
}

.table {
    width: 100%;
    font-size: 14px;
}

.red {
    color: #ff0000;
}

.mr10 {
    margin-right: 10px;
}

.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}

.everyDay {
    display: inline-block;
    width: 20px;
    height: 20px;
    background-color: #409eff;
    color: #fff;
    border-radius: 50%;
}
</style>
